﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace ExportToDWGTool
{

    class ReadDataFromMDB
    {
        public OleDbConnection m_Conn = null;

        public Dictionary<string, JingInfo> m_DicJ = new Dictionary<string, JingInfo>();
        public Dictionary<string, TongDaoInfo> m_DicTD = new Dictionary<string, TongDaoInfo>();

        private bool ConnectMDB(string strPathDB)
        {
            try
            {             
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPathDB;

                OleDbConnection conn = new OleDbConnection(strConn); //Jet OLEDB:Database Password=

                if (conn != null) m_Conn = conn;

                return true;
            }
            catch (Exception pException)
            {
                MessageBox.Show(pException.Message);
            }

            return false;

        }

        public bool ReadJingTableData(string strDBPath)
        {
            m_DicJ.Clear();

            try
            {
                if(ConnectMDB(strDBPath))
                {
                    if (m_Conn == null)
                    {
                        MessageBox.Show("数据库连接异常！");
                        return false;
                    }
                    m_Conn.Open();

                    OleDbCommand cmd = m_Conn.CreateCommand();
                
                    string sql = "select *";
                       
                    cmd.CommandText = string.Format("{0} from Jing ", sql);
                
               
                    OleDbDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        string wID = dr["W_ID"].ToString();
                        string wMC = dr["W_NAME"].ToString();
                        //string wTP = dr["W_TP"].ToString();
                        string wCZ = dr["W_CZ"].ToString();
                        //string wXCBH = dr["W_XCBH"].ToString();
                        string wJLB = dr["W_JLB"].ToString();
                        string wJSL = dr["W_JSL"].ToString();
                        string wXZ = dr["W_XZ"].ToString();
                        //string wJBJ = dr["W_JBJ"].ToString();
                        string wJC = dr["W_JC"].ToString();
                        string wJK = dr["W_JK"].ToString();
                        string wJS = dr["W_JS"].ToString();
                       // string wJNS = dr["W_JNS"].ToString();
                        string wGBCC = dr["W_GBCC"].ToString();
                        string wJGN = dr["W_JGN"].ToString();
                        string wJLX = dr["W_JLX"].ToString();
                        string wX = dr["W_X"].ToString();
                        string wY = dr["W_Y"].ToString();
                        string wRQ = dr["W_XZSJ"].ToString();
                        if (!m_DicJ.Keys.Contains(wID))
                        {
                            JingInfo pWInfo = new JingInfo();
                            pWInfo.w_ID = wID;
                            pWInfo.w_MC = wMC;
                            //pWInfo.w_TP = wTP;
                            pWInfo.w_CZ = wCZ;
                           // pWInfo.w_XCBH = wXCBH;
                            pWInfo.w_JLB = wJLB;
                            pWInfo.w_JSL = wJSL;
                            pWInfo.w_XZ = wXZ;
                           // pWInfo.w_JBJ = wJBJ;
                            pWInfo.w_JC = wJC;
                            pWInfo.w_JK = wJK;
                            pWInfo.w_JS = wJS;
                           // pWInfo.w_JNS = wJNS;
                            pWInfo.w_GBCC = wGBCC;
                            pWInfo.w_JGN = wJGN;
                            pWInfo.w_JLX = wJLX;
                            pWInfo.w_X = wX;
                            pWInfo.w_Y = wY;

                            pWInfo.w_RQ = wRQ;

                            //查询断面信息

                            pWInfo.m_DicDM = new Dictionary<string, DuanMianInfo>();

                            ReadDMTableData(wID, ref pWInfo);
                            m_DicJ.Add(wID, pWInfo);
                        }

                    }
                    cmd.Dispose();
                    m_Conn.Close();

                }

                return true;
            }
            catch(Exception ex)
            {
                m_Conn.Close();
            }

            return false;
        }

        public bool ReadDMTableData(string strWID,ref JingInfo pWInfo)
        {
            try
            {
                if (m_Conn == null)
                {
                    MessageBox.Show("数据库连接异常！");
                    return false;
                }

                OleDbCommand cmd = m_Conn.CreateCommand();

                string sql = "select *";

                cmd.CommandText = string.Format("{0} from Poumian where W_ID = '{1}' ", sql, strWID);
   
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {              
                    string sID = dr["S_ID"].ToString();
                    string sMC = dr["S_MC"].ToString(); 
                    string sTP = dr["S_TP"].ToString();
                    string sFW = dr["S_FW"].ToString();
                    string sXH = dr["S_XH"].ToString();
                    string sSBJ = dr["S_SBJ"].ToString();
                    string sXBJ = dr["S_XBJ"].ToString();
                    string sZBJ = dr["S_ZBJ"].ToString();
                    string sYBJ = dr["S_YBJ"].ToString();
                    string sHJJ = dr["S_HJJ"].ToString();
                    string sLJJ = dr["S_LJJ"].ToString();
                    string sH = dr["S_H"].ToString();
                    string sL = dr["S_L"].ToString();

                    DuanMianInfo pSInfo = new DuanMianInfo();      
                    pSInfo.s_ID = sID;
                    pSInfo.s_MC = sMC;
                    pSInfo.s_TP = sTP;
                    pSInfo.s_FW = sFW;
                    pSInfo.s_XH = sXH;
                    pSInfo.s_SBJ = sSBJ;
                    pSInfo.s_XBJ = sXBJ;
                    pSInfo.s_ZBJ = sZBJ;
                    pSInfo.s_YBJ = sYBJ;
                    pSInfo.s_H = sH;
                    pSInfo.s_L = sL;
                    pSInfo.s_HJJ = sHJJ;
                    pSInfo.s_LJJ = sLJJ;

                    //查询孔位信息
                    ReadKWTableData(sID,ref pSInfo);

                    if (pWInfo.m_DicDM != null)
                    {
                        if (!pWInfo.m_DicDM.Keys.Contains(sID))
                        {
                            pWInfo.m_DicDM.Add(sID, pSInfo);
                        }
                    }
                    else
                    {
                        pWInfo.m_DicDM = new Dictionary<string, DuanMianInfo>();
                        pWInfo.m_DicDM.Add(sID, pSInfo);
                    }
                }

                cmd.Dispose();

                return true;
            }
            catch (Exception ex)
            {

            }

            return false;
        }

        public bool ReadKWTableData(string strSID,ref DuanMianInfo pSInfo)
        {
            try
            {
                if (m_Conn == null)
                {
                    MessageBox.Show("数据库连接异常！");
                    return false;
                }

                pSInfo.s_maxGJ = 0;
                OleDbCommand cmd = m_Conn.CreateCommand();

                string sql = "select *";

                cmd.CommandText = string.Format("{0} from Kongwei where S_ID = '{1}' ", sql, strSID);

                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {               
                    string hID = dr["H_ID"].ToString();
                    string hMC = dr["H_MC"].ToString();
                    string hXCBH = dr["H_XCBH"].ToString();
                    string hLX = dr["H_LX"].ToString();
                    string hGJ = dr["H_GJ"].ToString();
                    string hH = dr["H_H"].ToString();
                    string hL = dr["H_L"].ToString();
                    string hCZ = dr["H_CZ"].ToString();                      
               
                    KongWeiInfo pHInfo = new KongWeiInfo();
                    pHInfo.h_ID = hID;
                    pHInfo.h_MC = hMC;
                    pHInfo.h_XCBH = hXCBH;
                    pHInfo.h_LX = hLX;
                    pHInfo.h_GJ = hGJ;
                    pHInfo.h_H = hH;
                    pHInfo.h_L = hL;
                    pHInfo.h_CZ = hCZ;

                    if(pSInfo.s_maxGJ<double.Parse(hGJ))
                    {
                        pSInfo.s_maxGJ = double.Parse(hGJ);
                    }

                    if(pSInfo.m_DicKW!=null)
                    {
                        if (!pSInfo.m_DicKW.Keys.Contains(hH + "_" + hL))
                        {
                            pSInfo.m_DicKW.Add(hH + "_" + hL, pHInfo);
                        }                       
                    }
                    else
                    {
                        pSInfo.m_DicKW = new Dictionary<string, KongWeiInfo>();
                        pSInfo.m_DicKW.Add(hH + "_" + hL, pHInfo);
                    }
                   
                }
                cmd.Dispose();

                return true;
            }
            catch (Exception ex)
            {
            }

            return false;
        }

        public bool ReadTongdaoTbData(string strDBPath)
        {
            try
            {
                if (ConnectMDB(strDBPath))
                {
                    if (m_Conn == null)
                    {
                        MessageBox.Show("数据库连接异常！");
                        return false;
                    }
                    m_Conn.Open();

                    OleDbCommand cmd = m_Conn.CreateCommand();

                    string sql = "select *";

                    cmd.CommandText = string.Format("{0} from Tongdao ", sql);

                
                    OleDbDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        string tID = dr["uuid"].ToString();
                        string tMC = dr["name"].ToString();
                        string tSID = dr["StartID"].ToString();
                        string tSMC = dr["StartMC"].ToString();
                        string tSType = dr["StartType"].ToString();
                        string tSX = dr["StartJD"].ToString();
                        string tSY = dr["StartWD"].ToString();
                        string tEID = dr["EndID"].ToString();
                        string tEMC = dr["EndMC"].ToString();
                        string tEType = dr["EndType"].ToString();
                        string tEX = dr["EndJD"].ToString();
                        string tEY = dr["EndWD"].ToString();
                        string tSL = dr["Gdsl"].ToString();
                        string tType = dr["Type"].ToString();                       

                        if (!m_DicTD.Keys.Contains(tID))
                        {
                            TongDaoInfo pTInfo = new TongDaoInfo();
                            pTInfo.t_ID = tID;
                            pTInfo.t_MC = tMC;
                            pTInfo.t_SID = tSID;
                            pTInfo.t_SNM = tSMC;
                            pTInfo.t_SType = tSType;
                            pTInfo.t_SX = tSX;
                            pTInfo.t_SY = tSY;
                            pTInfo.t_EID = tEID;
                            pTInfo.t_ENM = tEMC;
                            pTInfo.t_EType = tEType;
                            pTInfo.t_EX = tEX;
                            pTInfo.t_EY = tEY;
                            pTInfo.t_Type = tType;

                            pTInfo.t_GDSL = tSL;

                            if(int.Parse(tSL)>0)
                            {
                                //查询拐点信息
                                ReadGDTableData(tID, ref pTInfo);                               
                            }
                            m_DicTD.Add(tID, pTInfo);
                        }
                    }
                    cmd.Dispose();
                    m_Conn.Close();

                }
                return true;
            }
            catch (Exception ex)
            {
                m_Conn.Close();
            }

            return false;
        }

        public bool ReadGDTableData(string strSID,ref TongDaoInfo pTInfo)
        {
            try
            {
                if (m_Conn == null)
                {
                    MessageBox.Show("数据库连接异常！");
                    return false;
                }

                OleDbCommand cmd = m_Conn.CreateCommand();

                string sql = "select *";

                cmd.CommandText = string.Format("{0} from Guaidian where G_TDID = '{1}' ", sql, strSID);

                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string gID = dr["G_ID"].ToString();
                    string gMC = dr["G_MC"].ToString();
                    string gMS = dr["G_MS"].ToString();                 
                    string gXH = dr["G_XH"].ToString();
                    string gX = dr["G_X"].ToString();
                    string gY = dr["G_Y"].ToString();
                  

                    GuaiDianInfo pGInfo = new GuaiDianInfo();
                    pGInfo.g_ID = gID;
                    pGInfo.g_MC = gMC;
                    pGInfo.g_MS = gMS;
                    pGInfo.g_XH = gXH;
                    pGInfo.g_X = gX;
                    pGInfo.g_Y = gY;
                   

                    if (pTInfo.m_DicGD != null)
                    {
                        if (!pTInfo.m_DicGD.Keys.Contains(gXH))
                        {
                            pTInfo.m_DicGD.Add(gXH, pGInfo);
                        }
                    }
                    else
                    {
                        pTInfo.m_DicGD = new Dictionary<string, GuaiDianInfo>();
                        pTInfo.m_DicGD.Add(gXH, pGInfo);
                    }

                }
                cmd.Dispose();

                return true;
            }
            catch (Exception ex)
            {
            }

            return false;
        }


    }
}
